package com.example.ppapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ppapp.domain.InfoBean;
import com.example.ppapp.view.AmountView;
import com.example.ppapp.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ItemShopListViewAdapter extends BaseAdapter {

    private List<InfoBean> infoBeans;

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemShopListViewAdapter(Context context, List<InfoBean> infoBeans) {
        this.context = context;
        this.infoBeans = infoBeans;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return infoBeans.size();
    }

    @Override
    public InfoBean getItem(int position) {
        return infoBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_shop_listview, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(position, (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final int position, ViewHolder holder) {
        final InfoBean infoBean = getItem(position);
        holder.amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                infoBean.setBuycount(amount);
                ItemShopListViewAdapter.this.notifyDataSetChanged();
            }
        });
        holder.btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Button) v).setText("已购");
                saveShopToCar(position);
            }
        });
        holder.amountView.setGoods_storage(infoBean.getCount());
        int buycount = infoBean.getBuycount();
        int price = buycount == 0 ? 1 : buycount;
        String s = "￥" + infoBean.getPrice() * price;
        holder.tv_price.setText(s);
        holder.tv_shop_title.setText(infoBean.getTitle());
        holder.tv_shop_kind.setText(infoBean.getKind());
        Picasso.with(context).load(infoBean.getPic()).error(R.mipmap.ic_launcher).into(holder.img);
    }

    private ArrayList<Integer> list = new ArrayList<>();

    private void saveShopToCar(int position) {
        list.add(position);
    }

    public ArrayList<Integer> getList() {
        return list;
    }

    protected class ViewHolder {
        private ImageView img;
        private AmountView amountView;
        private Button btnBuy;
        private TextView tv_price;
        private TextView tv_shop_kind;
        private TextView tv_shop_title;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            amountView = (AmountView) view.findViewById(R.id.amountView);
            btnBuy = (Button) view.findViewById(R.id.btn_buy);
            tv_price = view.findViewById(R.id.tv_price);
            tv_shop_kind = view.findViewById(R.id.tv_shop_kind);
            tv_shop_title = view.findViewById(R.id.tv_shop_title);
        }
    }
}